package edu.tomee.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.tomee.db.DatabaseConnection;
import edu.tomee.db.UserInfo;
import edu.tomee.mail.MailUtil;

/**
 * Servlet implementation class DisplayServlet
 */
@WebServlet("/display")
public class DisplayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	DatabaseConnection db = new DatabaseConnection();
    public DisplayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		int msg = 0;
		UserInfo ui = new UserInfo();
		//String uid = request.getParameter("T4");
		String fnm = request.getParameter("T5");
		String lnm = request.getParameter("T1");
		String phn = request.getParameter("T3");
		String eid = request.getParameter("T2");
		String pwd = request.getParameter("T6");
		String addr = request.getParameter("S1");
		
		try {
			msg = db.insert( fnm, lnm, phn, eid, pwd, addr);
			ui.setAddrs(addr);
			ui.setEid(eid);
			ui.setFname(fnm);
			ui.setLname(lnm);
			ui.setPhn(phn);
			ui.setPwd(pwd);
			StringBuilder ms = new StringBuilder();
			ms.append("Welcome to KBase. <br /> Thank you for registering with us <br />");
			ms.append("Your email id is : <b>").append(eid).append("</b> <br/ >" );
			ms.append("Please <a href='http://tomee-openhouse.rhcloud.com/login'> login </a> here ");
			MailUtil.sendMessage(eid, ms.toString(), "Registration Confirmation");
		} catch (Exception e) {
			out.println(e.getMessage());
		}
		request.setAttribute("msg", msg);
		request.setAttribute("ui", ui);
		request.getRequestDispatcher("displayUsingJstl.jsp").forward(request, response);

	}

}
